package com.AAL.Action;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.SessionAware;

import com.AAL.DAO.AccountInformationDAO;
import com.AAL.DAO.AccountsDAO;
import com.AAL.DAO.CityDAO;
import com.AAL.DAO.DealerDAO;
import com.AAL.DAO.RegionDAO;
import com.AAL.DAO.ScaleDAO;
import com.AAL.DAO.StatusOfAccountDAO;
import com.AAL.Model.Account;
import com.AAL.Model.AccountInformation;
import com.AAL.Model.City;
import com.AAL.Model.Dealer;
import com.AAL.Model.Region;
import com.AAL.Model.Scale;
import com.AAL.Utils.DateUtil;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

@SuppressWarnings("serial")
public class AccountAction extends ActionSupport implements SessionAware {

	/*
	 * Khao bao cac thuoc tinh
	 */

	private Account account;
	private List<Region> lstRegion;
	private List<City> lstCities;
	private List<Scale> lstScales;

	private RegionDAO lnkRegion;
	private CityDAO lnkCity;
	private AccountsDAO lnkAccount;
	private DealerDAO lnkDealerDAO;
	private AccountInformationDAO lnkAccountInformationDAO;
	private StatusOfAccountDAO lnStatusOfAccountDAO;
	private ScaleDAO lnkScaleDAO;
	private Boolean checkUsername;
	private Boolean checkEmail;

	private Map<String, Object> session;

	public AccountAction() {
		this.lnkAccount = new AccountsDAO();
		this.lnkCity = new CityDAO();
		this.lnkRegion = new RegionDAO();
		this.lnkAccountInformationDAO = new AccountInformationDAO();
		this.lnkDealerDAO = new DealerDAO();
		this.lnStatusOfAccountDAO = new StatusOfAccountDAO();
		this.lnkScaleDAO = new ScaleDAO();
		

	}

	/*
	 * ------------------------------------------------------------- 
	 * Minh
	 * "javascript:return confirm('Delete all selected roles?')"
	 * -------------------------------------------------------------
	 */

	public String login() {
		try {			
			account = lnkAccount.login(account.getUserName(),
					account.getPassword());
			
			if (account != null) {
				session.put("myAccount", account);
				session.get("myAccount");
			} else {
				addActionError("Tài khoản bạn chưa được kích hoạt hoặc chưa tồn tại tài khoản này trong hệ thống.");
				return ERROR;
			}
		} catch (Exception e) {
			addActionError("Error:" + e.getMessage());
			return ERROR;
		}
		return SUCCESS;
	}

	public String logout() {
		try {
			session.clear();
		} catch (Exception e) {
			return ERROR;
		}
		return SUCCESS;
	}

	public String preRegister_step1() {
		try {
			System.out.println("Register - step 1");
			
			 /*setLstRegion(lnkRegion.getALlList()); */
			List<Region> lstRegionT = new ArrayList<Region>();
			lstRegionT = lnkRegion.getALlList();
			this.lstRegion = new ArrayList<Region>();
			for (Region item : lstRegionT) {
				Region a = new Region();
				a.setRegionID(item.getRegionID());
				a.setRegionName(item.getRegionName());
				this.lstRegion.add(a);
			}
			System.out.println("Region: "+this.lstRegion);
		} catch (Exception e) {
			return ERROR;
		}
		return SUCCESS;
	}

	public String preRegister_step2() {
		HttpServletRequest request = (HttpServletRequest) ActionContext
				.getContext().get(ServletActionContext.HTTP_REQUEST);
		try {
			System.out.println("Register - step 2");
			List<City> lstCitiesTemp = lnkCity.getLstCityByRegionID(Integer
					.parseInt(request.getParameter("region")));

			this.lstCities = new ArrayList<City>();
			for (City city : lstCitiesTemp) {
				City a = new City();
				a.setCityID(city.getCityID());
				a.setCityName(city.getCityName());
				this.lstCities.add(a);
			}

		} catch (Exception e) {
			return ERROR;
		}
		return SUCCESS;
	}

	// ay danh sach qui mo cong ty
	public String preRegister_step3() {
		try {
			System.out.println("Register - step 3");
			session.clear();
			/* setLstRegion(lnkRegion.getALlList()); */
			List<Scale> lstScaleT = new ArrayList<Scale>();
			lstScaleT = lnkScaleDAO.getAllScales();
			this.lstScales = new ArrayList<Scale>();
			for (Scale scale : lstScaleT) {
				Scale a = new Scale();
				a.setScaleID(scale.getScaleID());
				a.setScaleDealer(scale.getScaleDealer());
				this.lstScales.add(a);
			}
		} catch (Exception e) {
			return ERROR;
		}
		return SUCCESS;
	}

	public String preRegister_step4() {
		HttpServletRequest request = (HttpServletRequest) ActionContext
				.getContext().get(ServletActionContext.HTTP_REQUEST);
		try {
			Account accCheck = lnkAccount.checkUserName(request.getParameter(
					"uName").trim());
			if (accCheck != null) {
				checkUsername = false;
			} else {
				checkUsername = true;
			}
		} catch (Exception e) {
			return ERROR;
		}
		return SUCCESS;
	}

	public String preRegister_step5() {
		HttpServletRequest request = (HttpServletRequest) ActionContext
				.getContext().get(ServletActionContext.HTTP_REQUEST);
		try {
			Account accCheck = lnkAccount.checkEmail(request.getParameter(
					"cEmail").trim());
			if (accCheck != null) {				
				checkEmail = false;
			} else {
				checkEmail = true;
			}
		} catch (Exception e) {
			return ERROR;
		}
		return SUCCESS;
	}

	public String finishRegister() {
		HttpServletRequest request = (HttpServletRequest) ActionContext
				.getContext().get(ServletActionContext.HTTP_REQUEST);
		try {

			account.setCreationDate(DateUtil.getToday());
			// trang thai de-Active
			account.setStatusOfAccount(lnStatusOfAccountDAO
					.getStatusOfAccount(2));
			account.setPassword(request.getParameter("password").trim());
			//account = lnkAccount.saveAndUpdateAccount(account);			
			//AccountInformation accInfoTemp = this.account.getAccountInformation();
			//accInfoTemp.setAccount(account);
			//lnkAccountInformationDAO.saveAndUpdateAccountInformation(accInfoTemp);			
			Account accountT = this.account;
			AccountInformation accInforT = this.account.getAccountInformation();
			accInforT.setAccount(accountT);
			Dealer dealerTemp = this.account.getDealer();			
			//dealerTemp.setAccount(account);
			dealerTemp.setAccount(accountT);
			dealerTemp.setOutstanding(new BigDecimal(0));
			dealerTemp = lnkDealerDAO.saveAndUpdateDealer(dealerTemp);
			if (dealerTemp == null) {
				addActionError("Lỗi: Không thể tạo tài khoản. Hãy liên lạc với công ty.");
				return ERROR;
			}

		} catch (Exception e) {
			addActionError("Lỗi: " + e.getMessage());
			return ERROR;
		}
		return SUCCESS;
	}

	/*
	 * ----------------------------------------------------------- 
	 * Ket thuc phan cua Minh
	 * ----------------------------------------------------------
	 */

	@Override
	public void setSession(Map<String, Object> arg0) {
		this.session = arg0;

	}

	public Map<String, Object> getSession() {
		return session;
	}

	public Account getAccount() {
		return account;
	}

	public void setAccount(Account account) {
		this.account = account;
	}

	public List<Region> getLstRegion() {
		return lstRegion;
	}

	public void setLstRegion(List<Region> lstRegion) {
		this.lstRegion = lstRegion;
	}

	public List<City> getLstCities() {
		return lstCities;
	}

	public void setLstCities(List<City> lstCities) {
		this.lstCities = lstCities;
	}

	public List<Scale> getLstScales() {
		return lstScales;
	}

	public void setLstScales(List<Scale> lstScales) {
		this.lstScales = lstScales;
	}

	public Boolean getCheckUsername() {
		return checkUsername;
	}

	public void setCheckUsername(Boolean checkUsername) {
		this.checkUsername = checkUsername;
	}

	public Boolean getCheckEmail() {
		return checkEmail;
	}

	public void setCheckEmail(Boolean checkEmail) {
		this.checkEmail = checkEmail;
	}

}
